<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="commodityNo">
        <el-input v-model="queryParams.commodityNo" placeholder="请输入商品编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称" prop="commodityName1">
        <el-input v-model="queryParams.commodityName1" placeholder="请输入商品名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品类型" prop="commodityType">
        <el-cascader :options="options" v-model="queryParams.commodityType" :props="props" clearable></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <!--商品类型数据-->
      <el-col :span="3" :xs="24">
        <div class="head-container">
          <el-tree :data="commodityTypeOptions" :props="defaultProps" :expand-on-click-node="false" ref="tree"
            node-key="id" default-expand-all highlight-current @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--商品数据-->
      <el-col :span="21" :xs="24">
        <el-table v-loading="loading" ref="multipleTable" row-key="commodityId" :data="ErpCommodityList" @selection-change="handleSelectionChange" border>
          <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
          <el-table-column label="商品编号" align="center" prop="commodityNo" />
          <el-table-column label="商品名称" align="center" prop="commodityName1" width="200px;"/>
          <el-table-column label="商品类别" align="center" prop="commodityType" />
          <el-table-column label="品牌" align="center" prop="commodityBrand" />
          <el-table-column label="规格" align="center" prop="commoditySpec" />
          <el-table-column label="基本单位" align="center" prop="unitName1" />
          <el-table-column label="成本价" align="center" prop="costPrice" >
            <template slot-scope="scope">
              <span>{{ formattedNumber(scope.row.costPrice) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="当前库存" align="center" prop="wareCommodityNumber"/>
<!--            <template slot-scope="scope">-->
<!--              &lt;!&ndash; <span v-if="scope.row.wareCommodityNumber">-->
<!--                {{ scope.row.wareCommodityNumber }}-->
<!--              </span>-->
<!--              <span v-else>-->
<!--                {{ numberSum(scope.row.numberList) }}-->
<!--                <el-tooltip placement="bottom" effect="light">-->
<!--                  <div slot="content">-->
<!--                    <div v-for="item in scope.row.numberList" :key="item" v-if="item.wareId">-->
<!--                      <span>{{ item.wareName }}：</span><span>{{ item.commodityNumber-->
<!--                        }}</span>-->
<!--                    </div>-->
<!--                  </div>-->
<!--                  <el-button icon="el-icon-search" size="mini" circle></el-button>-->
<!--                </el-tooltip>-->
<!--              </span> &ndash;&gt;-->
<!--&lt;!&ndash;              <span>&ndash;&gt;-->
<!--&lt;!&ndash;                {{ numberSum(scope.row.numberList) }}&ndash;&gt;-->
<!--&lt;!&ndash;                <el-tooltip placement="bottom" effect="light">&ndash;&gt;-->
<!--&lt;!&ndash;                  <div slot="content">&ndash;&gt;-->
<!--&lt;!&ndash;                    <div v-for="item in scope.row.numberList" :key="item" v-if="item.wareId &&  item.commodityNumber > 0">&ndash;&gt;-->
<!--&lt;!&ndash;                      <span>{{ item.wareName }}：</span><span>{{ item.commodityNumber&ndash;&gt;-->
<!--&lt;!&ndash;                        }}</span>&ndash;&gt;-->
<!--&lt;!&ndash;                    </div>&ndash;&gt;-->
<!--&lt;!&ndash;                  </div>&ndash;&gt;-->
<!--&lt;!&ndash;                  <el-button icon="el-icon-search" size="mini" circle></el-button>&ndash;&gt;-->
<!--&lt;!&ndash;                </el-tooltip>&ndash;&gt;-->
<!--&lt;!&ndash;              </span>&ndash;&gt;-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="库存上限" align="center" prop="inventoryLimit" />
          <el-table-column label="库存下限" align="center" prop="inventoryLowerLimit" />
          <el-table-column label="描述" align="center" prop="commodityDescribe" />
        </el-table>
        <pagination  v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listErpCommodity } from "@/api/erp/ErpCommodity";
import { listErpCommodityType } from "@/api/erp/ErpCommodityType";
import {formattedNumber} from "@/utils/ruoyi";
export default {
  name: "ErpCommodity",
  props: {
    wareId: {
      type: Number
    },
    notCommodityIds: {
      type: Array
    },
    isSymbol: {
      type: String
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品管理表格数据
      ErpCommodityList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        commodityNo: null,
        commodityName1: null,
        commodityName2: null,
        commodityType: null,
        commodityStatus: 1,
        notCommodityIds: this.notCommodityIds?.join(','),
        params: {
          wareId: this.wareId,
          isSymbol: this.isSymbol
        }
      },
      // 商品类别配置选项
      props: {
        checkStrictly: true,
        value: 'commodityTypeId',
        label: 'commodityTypeName'
      },
      // 左侧商品类别树
      commodityTypeOptions: [],
      // 下拉商品类型
      options: [],
      defaultProps: {
        children: "children",
        label: "commodityTypeName"
      },
    };
  },
  created() {
    this.getList();
    this.getListCommodityType();
  },
  methods: {
    formattedNumber,
    /** 查询商品管理列表 */
    getList() {
      this.loading = true;
      listErpCommodity(this.queryParams).then(response => {
        this.ErpCommodityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取商品类别 */
    getListCommodityType() {
      listErpCommodityType().then(response => {
        this.options = this.handleTree(response.data, "commodityTypeId", "commodityTypePid");
        this.commodityTypeOptions = [];
        const data = { commodityTypeId: 0, commodityTypeName: '全部', children: [] };
        data.children = this.handleTree(response.data, "commodityTypeId", "commodityTypePid");
        this.commodityTypeOptions.push(data);
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commodityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.$emit('SelectionChange', selection);
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.commodityType = data.commodityTypeId == 0 ? null : data.commodityTypeId;
      this.handleQuery();
    },
    /** 数值累计 */
    numberSum(list) {
      let sum = 0;
      list.forEach(i => {
        sum += i.commodityNumber;
      })
      return sum;
    },
  }
};
</script>
<style lang="css" scoped>
  ::v-deep .el-table__cell{
    padding: 0px;
  }
</style>
